<template>
	<view>
		<view style="position: relative;">
			<swiper 
				style="height: 300px;" 
				indicator-dots
				:current="current" 
				@change="changeSwiper"
			>
				<swiper-item style="display: flex; justify-content: center; align-items: center;" v-for="(item, index) of banners" :key="index">
					<image  :src="item" mode=""  @click="previewImage"/>
				</swiper-item>
			</swiper>
			<view style="position: absolute; bottom: 20px;right: 0px;width: 60px;height: 30px;background-color: #ccc; border-radius: 15px 0 0 15px;text-align: center;line-height: 30px;">
				{{ current + 1 }}/ {{ banners.length}}
		  </view>
		</view>
		<view>{{ proname }}</view>
		<view>￥{{ originprice }}</view>
		<!-- 底部占据空间空出来，足够的内容的展示空间 -->
		<view style="height: 50px"></view>
		<view class="goods-carts">
			<uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import { addCartFn } from '../../api/cart.js'
	import { getProDetail } from '../../api/detail.js'
	export default {
		data() {
			return {
				proid: '',
				banners: [],
				proname: '',
				originprice: 0,
				current: 2,
				options: [{
					icon: 'chat',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "#f5f5f5"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}
				],
			}
		},
		onLoad({ proid }) {
			getProDetail(proid).then(res => {
				console.log(res.data)
				this.banners = res.data.data.banners[0].split(',')
				this.proid = proid
				this.proname = res.data.data.proname
				this.originprice = res.data.data.originprice
			})
		},
		methods: {
			changeSwiper ({ detail: { current }}) {
				this.current = current
			},
			previewImage () {
				uni.previewImage({
					urls: this.banners,
					current: this.banners[this.current]
				})
			},
			onClick(e) {
				uni.showToast({
					title: `点击${e.content.text}`,
					icon: 'none'
				})
				switch (e.content.text){
					case '客服':
						break;
					case '店铺':
						break;
					case '购物车':
					if (uni.getStorageSync("token")) {
						uni.switchTab({
							url: '/pages/cart/cart'
						})
					} else {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
						break;
					default:
						break;
				}
			},
			buttonClick(e) {
				switch (e.index){
					case 0:
						if (uni.getStorageSync("token")) {
							addCartFn({
								userid: uni.getStorageSync("userid"),
								proid: this.proid,
								num: 1
							}).then(() => {
								uni.showToast({
									title: '加入购物车成功',
								})
							})
						} else {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}
						break;
					case 1:
						break;
					default:
						break;
				}
			}
		},
	}
</script>

<style>
	.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
</style>